<?php
error_reporting(0);
session_start();
include_once '../comm/config.php';
include_once '../comm/checkpostandget.php';
include_once 'check.php';
include_once '../comm/wjt.php';
include_once '../comm/msgset.php';

$yzm=$_POST['yzm']; 
$id=(int)$_POST["id"]; 
$merchant_id=$_POST["merchant_id"]; 
$trade_id=$_POST["jyh"]; 
if($yzm==""||$yzm!=$_SESSION["code"])
{
	echo "<script language=javascript>window.location='error.php?id=15';</script>";
	exit;
}
else
{	
		$sql="select * from ".$BIAOTOU."mall_order where id='".$id."' and (u_id='' or u_id is NULL )";
		$rs = mysql_query($sql);
		while($row = mysql_fetch_object($rs)) 
		{
			$order_code=$row->order_code;
			$dengji=$_SESSION["dduserlevel"];
			$fxje=mall_ck_fxje($row->commission,$dengji); 
			$tgje=round(($row->commission)*TGBL,2); 
		}
		if($trade_id==$order_code)
		{
			$sj= date("Y-m-d H:i:s");  
			//交易列表增加确认人，根据会员等级，更新返现金额
			$sql="update ".$BIAOTOU."mall_order set u_id='".$_SESSION["dduserid"]."',`ddusername`='".$_SESSION["duoduouser"]."',`fxje`='$fxje' where id='".$id."' and (u_id='' or u_id is NULL )";
			mysql_query($sql);
			
			//查看此订单的状态，如果是核对有效，就给会员返现
			$sql="select stat_desc from ".$BIAOTOU."mall_order where id=".$id;
			$query=mysql_query($sql);
			$stat_desc=mysql_fetch_array($query);
			if($stat_desc[0]=='核对有效'){
			    //用户佣金
			    $sql="update ".$BIAOTOU."user set money=money+".$fxje.",dengji=dengji+1 where Id='".$_SESSION["dduserid"]."'";
			    mysql_query($sql);
			    //用户消息
				$msg_tabao=str_replace("[trade_id]",$trade_id,$msg_tabao);
				$msg_tabao=str_replace("[fxje]",$fxje,$msg_tabao);
			    $sql="insert into ".$BIAOTOU."msg (title,content,addtime,see,ddusername,senduser) values ('获得交易返现','".$msg_tabao."','$sj',0,'".$_SESSION["duoduouser"]."','网站客服');";
			    //echo $sql;
			    mysql_query($sql);
			    //用户明细
			    $sql="insert into ".$BIAOTOU."mingxi (ddusername,shijian,je,addtime,memo) values ('".$_SESSION["duoduouser"]."','交易返现',".$fxje.",'$sj','".$merchant_id."交易号".$trade_id."')";
			    mysql_query($sql);
			    //取推荐人
			    $sqltjr="select tjr from ".$BIAOTOU."user where ddusername='".$_SESSION["duoduouser"]."'";
			    $tg=mysql_query($sqltjr);
			    while ($tt = mysql_fetch_array($tg)){$tjr=$tt["tjr"]; }	
			    if($tjr>0)
			    {   			
				    //推广人佣金
				    $sql="update ".$BIAOTOU."user set money=money+".$tgje." where Id='".$tjr."'";
				    mysql_query($sql);
				    //取推荐人名
				    $sqltjr="select ddusername from ".$BIAOTOU."user where Id='".$tjr."'";
				    $tgr=mysql_query($sqltjr);
				    while ($tu = mysql_fetch_array($tgr)){
					    $tjrusername=$tu["ddusername"]; }	
				    //推广人明细
				    $sql="insert into ".$BIAOTOU."mingxi (ddusername,shijian,je,addtime,memo) values ('".$tjrusername."','推广佣金',".$tgje.",'$sj','交易人".$_SESSION["duoduouser"]."')";
				    mysql_query($sql);
				    //推广人消息
					$ddusername=$_SESSION["duoduouser"];
					$msg_taobaotuiguang=str_replace("[ddusername]",$tjrusername,$msg_taobaotuiguang);
					$msg_taobaotuiguang=str_replace("[trade_id]",$tbjyh,$msg_taobaotuiguang);
					$msg_taobaotuiguang=str_replace("[tgje]",$tgje,$msg_taobaotuiguang);
				    $sql="insert into ".$BIAOTOU."msg (title,content,addtime,see,ddusername,senduser) values ('您获得了新的推广佣金！','".$msg_taobaotuiguang."','$sj',0,'".$tjrusername."','网站客服')";
				    mysql_query($sql);
			    }  
				$msg="恭喜您，".$merchant_id."交易号为".$trade_id."的交易已经确认完毕，您获得了".$fxje."元的交易返现！ <br><br><br><br><a href=\"mingxilist.php?act=in\" class=\"redlink\">查看明细</a>";
			}
			else
		    {
			    $msg="恭喜您，".$merchant_id."交易号为".$trade_id."的交易已经确认完毕，请您耐心等待审核！ <br><br><br><br><a href=\"tradelist.php?act=mall\" class=\"redlink\">查看商城订单</a>";
			}
		}
		else
		{
				echo "<script language=javascript>window.location='error.php?id=28';</script>";
				exit;
		}		
$tag="center";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<title>找回订单 - 用户中心</title>
<link href="../css/usercss.css" rel="stylesheet" type="text/css">
<link href="../template/<?=MOBAN?>/css/common.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="../js/jquery.js"></script>
</head>

<body id="tradelist">
<?php
include("../template/".MOBAN."/header.tpl.php");
?>
<?php
include("../template/".MOBAN."/nav.tpl.php");
?>
<div id="main">
<?php
include("left.php");
?>
<div class="contain">
  <h2 class="left_nav2">找回订单</h2>
    <div class="order">
    <div class="content" style="font-size:12px; height:200px;">
  <table width="680" border="0" align="center" cellpadding="0" cellspacing="0">
                        <tr>
                          <td height="145" colspan="3" align="center">&nbsp;<?=$msg?></td>
                        </tr>
        </table>
</div>
	</div>
</div>
</div>
<?php
include("../template/".MOBAN."/footer.tpl.php");
?>
</body>
</html>